﻿using System;
using System.IO;
using System.Text;
namespace program08
{
    class Program
    {
        static void Main(string[] args)
        {
            var source = @"E:\作业\lbwnb\program07\bin\Debug\netcoreapp3.1\new.txt";
            var target = @"C:\Users\WIN10\Desktop\2.txt";
            CopyFile(source, target);
            Console.WriteLine("Copy Successed!");
        }

        public static void CopyFile(string source, string target)
        {
            ////读取文件
            //FileStream fsRead = new FileStream(source,
            //    FileMode.OpenOrCreate, FileAccess.Read);

            ////定义读取文件的大小不能超过5M；

            //byte[] buffer = new byte[1024 * 1024 * 5]; ;

            ////读取当前文件的有效字节数
            //int r = fsRead.Read(buffer, 0, buffer.Length);
            ////关闭文件流
            //fsRead.Close();

            ////使用FileStream来写入文件
            //using (FileStream fsWrite = new FileStream(target, FileMode.OpenOrCreate, FileAccess.Write))
            //{
            //    fsWrite.Write(buffer, 0, buffer.Length);
            //}
            //Console.WriteLine("文件信息复制成功！");

            //先读取文件流
            using (FileStream fsRead = new FileStream(source, FileMode.OpenOrCreate, FileAccess.Read))
            {
                //写入新文件
                using (FileStream fsWrite = new FileStream(target, FileMode.OpenOrCreate, FileAccess.Write))
                {
                    //读写文件的缓冲区
                    byte[] buffer = new byte[1024 * 1024 * 10];

                    //用循环读取文件流，如果源文件大于缓冲区，那么就要反复读取写入
                    while (true)
                    {
                        //返回本次读取的实际字节数
                        int r = fsRead.Read(buffer, 0, buffer.Length);
                        //如果读到0，说明读取完成，跳出循环
                        if (r == 0)
                            break;

                        //写入文件
                        fsWrite.Write(buffer, 0, r);
                    }
                }
            }
        }
    }
}
